package ae.pcfc.cldos.online.web.controller.client.company.ep;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import ae.pcfc.cldos.online.common.delegate.EntryPermitPaymentDelegate;
import ae.pcfc.cldos.online.common.delegate.FnpGSDelegate;
import ae.pcfc.cldos.online.common.exception.BadAppException;
import ae.pcfc.cldos.online.common.exception.FNPAppException;
import ae.pcfc.cldos.online.common.exception.PaymentException;
import ae.pcfc.cldos.online.common.vo.ep.peyment.EntryPermitPaymentInfoVO;
import ae.pcfc.cldos.online.common.vo.ep.peyment.PaymentAdviceRequestVO;
import ae.pcfc.cldos.online.fnp.service.client.vo.FNPParameterVO;
import ae.pcfc.cldos.online.web.Constants;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BasePageController;
import ae.pcfc.cldos.online.web.model.EPPaymentDataModel;
import ae.pcfc.cldos.online.web.services.fnp.FnpParametersCheckService;

/**
 * 
 * @author arosa
 *
 */
@Controller
@RequestMapping(value = ViewPath.__ENTRY_PERMIT_SUBMIT)
public class EntryPermitController extends BasePageController {

	private static final String BASE_EP_PAGE_VIEW_PATH = "client.company.ep.form";
	
	@Autowired
	@Qualifier("entryPermitPaymentDelegate")
	private EntryPermitPaymentDelegate entryPermitPaymentDelegate;
	
	@Autowired
	private FnpParametersCheckService fnpParametersCheckService;
	
	@Autowired
	@Qualifier("FnpGSDelegate")
	private FnpGSDelegate fnpGSDelegate;
	
	@ModelAttribute(value = Constants.__EP_APP_DATA_MODEL)
	public EPPaymentDataModel populateEpPaymentDataModel(@RequestParam String appId) throws BadAppException{
		EntryPermitPaymentInfoVO entryPermitPaymentInfoVO = new EntryPermitPaymentInfoVO();
		EPPaymentDataModel epPaymentDataModel = new EPPaymentDataModel();
		try{
		entryPermitPaymentInfoVO = entryPermitPaymentDelegate.getEntryPermitPaymentInfoByAppId(getUserVO(),
						encryptionDelegate.decodeAndDecrypt(appId));
		}catch(FNPAppException e){
			epPaymentDataModel.setError(true);
		}
		mapper.map(entryPermitPaymentInfoVO, epPaymentDataModel);
		epPaymentDataModel.setAppId(appId);
		return epPaymentDataModel;
	}
	
	@ModelAttribute
	public void populateBreadCrumbs(ModelMap model, @RequestParam String appId) {
		breadCrumbsFactoryService.populateEPPaymentBreadcrumb(model, appId);
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/form.htm")
	public ModelAndView show() throws Throwable {
		return new ModelAndView(BASE_EP_PAGE_VIEW_PATH);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/submitPayment.htm")
	public ModelAndView submitPayment(@RequestParam String appId, @RequestParam String fnpServiceCode,
			HttpServletRequest request,
			@ModelAttribute(value = Constants.__EP_APP_DATA_MODEL) EPPaymentDataModel epPaymentDataModel, BindingResult errors) 
			throws Throwable {
		
		List<FNPParameterVO> allParams = fnpGSDelegate.retreiveFNPParameters(Arrays.asList(new String[]{fnpServiceCode}));
		List<FNPParameterVO> notFeeled = fnpParametersCheckService.feelParamsAndGetNotFeeled(request, allParams);  
		if(notFeeled.size()==0){
			PaymentAdviceRequestVO generateEPPaymentAdviceRequestVO = 
					new PaymentAdviceRequestVO();
			generateEPPaymentAdviceRequestVO.setAppId(encryptionDelegate.decodeAndDecrypt(appId));
			generateEPPaymentAdviceRequestVO.setFnpServiceCode(fnpServiceCode);
			generateEPPaymentAdviceRequestVO.setUserVO(getUserVO());
			generateEPPaymentAdviceRequestVO.setFnpParams(allParams);
			
			try{
				entryPermitPaymentDelegate.completePayment(generateEPPaymentAdviceRequestVO);
			} catch (PaymentException e) {
				errors.reject("error.payment");
			} 
		} else {
			errors.reject("sdgsd", "Recomplete payment info please");
		}
		
		if(errors.hasErrors()){
			return new ModelAndView(BASE_EP_PAGE_VIEW_PATH);
		} else {
			return new ModelAndView(ViewPath.__REDIRECT+ViewPath.__ADMIN_TASKS+"/show.htm");
		}
	}
}
